//
//  YZXSelectDateViewController.m
//  Replenishment
//
//  Created by 尹星 on 2017/7/19.
//  Copyright © 2017年 ruwang. All rights reserved.
//

#import "YZXSelectDateViewController.h"
#import "YZXCalendarView.h"
#import "YZXCustomDateBottomView.h"
#import "YZXCalendarHelper.h"

#define IMSScreen_Width          [UIScreen mainScreen].bounds.size.width

#define IMSScreen_Height        [UIScreen mainScreen].bounds.size.height

#define iS_iPhoneX \
    ({BOOL isPhoneX = NO;\
if (@available(iOS 11.0, *)) {\
isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;\
}\
(isPhoneX); })

#define kIOS13StatusBarHeight (@available(iOS 13.0, *) ? [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size.height : [UIApplication sharedApplication].statusBarFrame.size.height)

//状态栏高度
#define kStatusBarHeight             (iS_iPhoneX ? kIOS13StatusBarHeight : 20.0f)

//导航栏高度
#define kTopHeight                   (kStatusBarHeight + 44.0)

//tabBar高度
#define kTabBarHeight                (iS_iPhoneX ? 83.0 : 49.0)

//iPhone X 底部安全区域
#define kBottomSafeHeight            (iS_iPhoneX ? 34.0 : 0.0)

static const CGFloat bottomView_height = 50.0;

@interface YZXSelectDateViewController () <YZXCalendarDelegate>

@property (nonatomic, strong) YZXCalendarHelper           *helper;
@property (nonatomic, strong) YZXCalendarView             *customCalendarView;
@property (nonatomic, strong) YZXCustomDateBottomView     *bottomView;

@end

@implementation YZXSelectDateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择日期";
    self.view.backgroundColor = [UIColor whiteColor];
    [self p_initView];
}

- (void)p_initView
{
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;


    [self.view addSubview:self.customCalendarView];
    [self.view addSubview:self.bottomView];
    
    if (self.startDate.length && self.endDate.length) {
        self.customCalendarView.dateArray = @[self.startDate,self.endDate];
    }
    self.bottomView.startTime = self.startDate;
    self.bottomView.endTime = self.endDate;
}

#pragma mark - <YZXCalendarDelegate>
//自定义时间
- (void)clickCalendarWithStartDate:(NSString *)startDate andEndDate:(NSString *)endDate
{
    if (startDate) {
        self.bottomView.startTime = startDate;
    }else {
        self.bottomView.startTime = nil;
    }
    
    if ([endDate isEqualToString:@"error"]) {
//        [self p_errorMessageContent:@"最多可选择31天" withYOffset:0];
        return;
    }
    
    if (endDate) {
        self.bottomView.endTime = endDate;
    } else {
        self.bottomView.endTime = nil;
    }
}

#pragma mark - 懒加载
- (YZXCalendarHelper *)helper
{
    if (!_helper) {
        _helper = [YZXCalendarHelper helper];
        _helper.minStartDate = @"2022-07-30";
        _helper.maxEndDate = @"2022-12-30";

    }
    return _helper;
}

//自定义日历
- (YZXCalendarView *)customCalendarView
{
    if (!_customCalendarView) {
        CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, IMSScreen_Height - bottomView_height - kTopHeight - kBottomSafeHeight);
        _customCalendarView = [[YZXCalendarView alloc] initWithFrame:rect withStartDateString:self.helper.customDateStartDate endDateString:self.helper.customDateEndDate];
        _customCalendarView.backgroundColor = [UIColor redColor];
        _customCalendarView.delegate = self;
        if (self.maxChooseNumber) {
            _customCalendarView.maxChooseNumber = self.maxChooseNumber;
        }
    }
    return _customCalendarView;
}

//底部视图
- (YZXCustomDateBottomView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[YZXCustomDateBottomView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.customCalendarView.frame), self.view.bounds.size.width, bottomView_height)];
        //确定 选择日期
        __weak typeof(self) weak_self = self;
        _bottomView.confirmBlock = ^(NSString *startTime, NSString *endTime) {
            if (weak_self.confirmTheDateBlock) {
                weak_self.confirmTheDateBlock(startTime,endTime);
            }
            [weak_self.navigationController popViewControllerAnimated:YES];
        };
    }
    return _bottomView;
}


@end
